package com.example.campuslink.viewModel

import android.content.Intent
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.campuslink.commen.db.invitation.InvitationTable
import com.example.campuslink.commen.db.message.MessageTable
import com.example.campuslink.commen.db.userInfo.UserInfoTable
import com.example.campuslink.commen.db.userNumber.UserNumberTable
import com.example.campuslink.commen.enums.MessageTypeEnum
import com.example.campuslink.commen.tools.Sp
import com.example.campuslink.commen.tools.SpKey
import com.example.campuslink.commen.tools.putExtraKey
import com.example.campuslink.ui.activity.FansAndAttentionActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class UserMainViewModel : BaseViewModel() {
    private val _userInfo = MutableLiveData<UserInfoTable?>()
    val userInfo get() = _userInfo
    private val _userNumber = MutableLiveData<UserNumberTable>()
    val userNumber get() = _userNumber
    private val _attentionInvitationLiveData = MutableLiveData<MutableList<String>>()
    val attentionInvitationLiveData = _attentionInvitationLiveData
    private val _fenSiteLiveData = MutableLiveData<MutableList<String>>()
    val fenSiteLiveData = _fenSiteLiveData

    fun getUserInfo(userNumber: String) {
        viewModelScope.launch(Dispatchers.IO) {
            val tempUserInfo = userInfoDao.getUserInfoByUserNumber(userNumber)
            if (tempUserInfo != null) {
                _userInfo.postValue(tempUserInfo)
            }
        }
    }

    fun getUserNumberInfo(userNumber: String) {
        viewModelScope.launch(Dispatchers.IO) {
            val tempUserNumber = userNumberDao.getUserNumber(userNumber)
            if (tempUserNumber != null) {
                _userNumber.postValue(tempUserNumber ?: return@launch)
            }
        }
    }

    fun getFenSiteList(userNumber: String) {
        viewModelScope.launch(Dispatchers.IO) {
            val numberInfo = userNumberDao.getUserNumber(
                userNumber
            )
            if (numberInfo != null) {
                val invitationList = numberInfo.fansList
                _fenSiteLiveData.postValue(invitationList)
            }
        }
    }

    fun jumpToFansAndAttentionActivity(jumpType: String, number: String) {
//        Sp.setString(
//            context,
//            SpKey.OTHER_NUMBER.key,
//            Sp.getString(context, SpKey.OTHER_NUMBER.key, "") ?: ""
//        )
        startActivity(Intent().apply {
            putExtra(putExtraKey.JUMP_TYPE.key, jumpType)
            putExtra(putExtraKey.USER_NUMBER.key, number)
//            putExtra(putExtraKey.IS_FIRST_JUMP.key, true)
        }, targetClass = FansAndAttentionActivity::class.java)
    }

    private val _publicInvitationLiveData = MutableLiveData<MutableList<InvitationTable>>()
    val publicInvitationLiveData = _publicInvitationLiveData
    fun getPublicInvitationList(nowNumber: String) {
        viewModelScope.launch(Dispatchers.IO) {
            val numberInfo = userNumberDao.getUserNumber(
                nowNumber
            )
            if (numberInfo != null) {
                val invitationList = numberInfo.publishList
                _publicInvitationLiveData.postValue(invitationList)
            }
        }
    }

    private val _attentionLiveData = MutableLiveData<MutableList<String>>()
    val attentionLiveData = _attentionLiveData


    fun removeAttentionAndFans(nowNumber: String) {
        viewModelScope.launch(Dispatchers.IO) {
            val userNumber = Sp.getString(context, SpKey.USER_NUMBER.key, "")
            val otherNumber = Sp.getString(context, nowNumber, "")
            val nowUserNumberTableInfo = userNumberDao.getUserNumber(userNumber ?: "")
            val otherUserNumberTableInfo = userNumberDao.getUserNumber(otherNumber ?: "")
            nowUserNumberTableInfo?.followList?.removeAll { it == otherNumber }
            otherUserNumberTableInfo?.fansList?.removeAll { it == userNumber }
            userNumberDao.updateUserNumber(nowUserNumberTableInfo ?: return@launch)
            userNumberDao.updateUserNumber(otherUserNumberTableInfo ?: return@launch)
        }
    }

    fun addAttentionAndFans(nowNumber: String) {
        viewModelScope.launch(Dispatchers.IO) {
            val userNumber = Sp.getString(context, SpKey.USER_NUMBER.key, "")
            val otherNumber = Sp.getString(context, nowNumber, "")
            val nowUserNumberTableInfo = userNumberDao.getUserNumber(userNumber ?: "")
            val otherUserNumberTableInfo = userNumberDao.getUserNumber(otherNumber ?: "")
            nowUserNumberTableInfo?.followList?.removeAll { it == otherNumber }
            otherUserNumberTableInfo?.fansList?.removeAll { it == userNumber }
            nowUserNumberTableInfo?.followList?.add(otherNumber ?: "")
            otherUserNumberTableInfo?.fansList?.add(userNumber ?: "")
            val schoolInfoTable =
                schoolInfoDao.getSchoolInfoBySchoolName(nowUserNumberTableInfo?.schoolName ?: "")
            if (schoolInfoTable != null) {
                var messageTable = MessageTable()
                messageTable.handNumber = otherNumber ?: ""
                messageTable.sendNumber = userNumber ?: ""
                messageTable.messageType = MessageTypeEnum.NEW_ADD_ATTENTION.value
                messageTable.messageTime = System.currentTimeMillis()
                messageTable.messageContent = "对方关注了你"
                messageTable.messageImage = ""
                MessageTable.insertWithId(messageDao, messageTable)
                schoolInfoTable?.messageList?.add(messageTable)
                schoolInfoDao.updateSchoolInfo(schoolInfoTable)
            }
            userNumberDao.updateUserNumber(nowUserNumberTableInfo ?: return@launch)
            userNumberDao.updateUserNumber(otherUserNumberTableInfo ?: return@launch)
            getAttentionInvitationList(userNumber ?: "")
            getFenSiteList(userNumber ?: "")
        }
    }

    fun getAttentionInvitationList(userNumber: String) {
        viewModelScope.launch(Dispatchers.IO) {
            val numberInfo = userNumberDao.getUserNumber(
                userNumber
            )
            if (numberInfo != null) {
                val invitationList = numberInfo.followList
                _attentionInvitationLiveData.postValue(invitationList)
            }
        }
    }
}